package com.bigdata.appliance.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.appliance.common.exception.BusinessException;
import com.bigdata.appliance.common.exception.ResponseCode;
import com.bigdata.appliance.common.utils.JwtUtil;
import com.bigdata.appliance.entity.User;
import com.bigdata.appliance.mapper.UserMapper;
import com.bigdata.appliance.service.IUserService;
import com.bigdata.appliance.vo.req.ChargeReqVO;
import com.bigdata.appliance.vo.req.GetWalletReqVO;
import com.bigdata.appliance.vo.req.LoginReqVO;
import com.bigdata.appliance.vo.req.RegisterReqVO;
import com.bigdata.appliance.vo.resp.LoginRespVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author appliance-shop
 * @since 2023-12-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private JwtUtil jwtUtil;

    @Override
    public void register(RegisterReqVO vo) {
        User user = query().select("id").last("limit 1").eq("username", vo.getUsername()).one();
        if (user != null) {
            throw new BusinessException(ResponseCode.ACCOUNT_ALREADY_EXISTS.getCode(), ResponseCode.ACCOUNT_ALREADY_EXISTS.getMessage());
        }
        User insertUser = new User();
        BeanUtils.copyProperties(vo, insertUser);
        insertUser.setPassword(SecureUtil.md5(vo.getPassword()));
        insertUser.setWallet(new BigDecimal(0));
        int result = userMapper.insert(insertUser);
        if (result != 1) {
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public LoginRespVO login(LoginReqVO vo) {
        User user = query().select("type", "wallet", "password", "id").last("limit 1").eq("username", vo.getUsername()).one();
        if (user == null) {
            throw new BusinessException(ResponseCode.ACCOUNT_NOT_FOUND.getCode(), ResponseCode.ACCOUNT_NOT_FOUND.getMessage());
        }
        if (!SecureUtil.md5(vo.getPassword()).equals(user.getPassword())) {
            throw new BusinessException(ResponseCode.ACCOUNT_ERROR.getCode(), ResponseCode.ACCOUNT_ERROR.getMessage());
        }
        LoginRespVO result = new LoginRespVO();
        result.setId(user.getId());
        result.setToken(jwtUtil.createToken(String.valueOf(user.getId()), null));
        result.setUsername(vo.getUsername());
        result.setWallet(user.getWallet());
        result.setType(user.getType());
        return result;
    }

    @Transactional
    @Override
    public void charge(ChargeReqVO vo) {
        User user = userMapper.selectById(vo.getUserId());
        user.setWallet(user.getWallet().add(vo.getAmount()));
        int result = userMapper.updateById(user);
        if (result != 1) {
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public BigDecimal getWallet(GetWalletReqVO vo) {
        return userMapper.selectById(vo.getUserId()).getWallet();
    }
}
